<?
    /* -*- Mode: PHP5; tab-width: 4; indent-tabs-mode: nil; basic-offset: 4 -*- */

    /*********************************************************
    *            --== InArch ==--
    *
    * @author Kulikov Alexey <a.kulikov@gmail.com>
    * @version n/a
    * @since 02.05.2005
    * @copyright essentialmind.com 2005
    *
    *********************************************************/
    
    /***
    * Class breadcrumbs
    ****
    * This class represents the breadcrumbs stack useful in 
    * various interfaces
    ****
    * @package usefulStuff
    * @author A Kulikov <a.kulikov@gmail.com>
    * @version n/a
    * @since 02.05.2005
    * @copyright essentialmind.com 2005
    ***/
    class breadcrumbs{
        
        /**
         * Data Holder
         *
         * @var array
         */
        protected $crumbs = array();
        
        /**
         * The URL relevant to which all breadcrumbs will be built
         *
         * @var string
         */
        protected $root;
        
        /**
         * Reference to the open database connection
         *
         * @var ADOConnection $db
         */
        protected $db;
        
        /* --------------- END VARIABLE DECLARATION --------------- */
        
        /**
         * Enter description here...
         *
         * @param ADOConnection $db
         * @param string $root
         */
        public function __construct($db,$root='/'){
            $this->db = $db;
            $this->setRoot($root);
        }
        
        
        /**
         * Creates an additional breadcrumb on the top of the stack
         *
         * @param string $name
         * @param string $link
         */
        public function push($name,$link){
            //root reference has to be fixed
            if($link == '/'){ 
                $link = null; 
            }
            
            //push the data on top of the stack
            $this->crumbs[] = array('title'=>$name,'url'=>$this->getRoot().$link);   
        }
        
        
        /**
         * Erases the last breadcrumbs from the stack
         *
         */
        public function pop(){
            $item = array_pop($this->crumbs);   
            $item['url'] = str_replace($this->getRoot(),'',$item['url']);
            return $item;
        }
        
        
        /**
         * Returns the whole stack of preadcrumbs
         *
         * @return array
         */
        public function getCrumbs(){
            return $this->crumbs;   
        }
        
        
        /**
         * Sets the root URL relative to which all breadcrumbs will be set
         *
         * @param string $root
         */
        public function setRoot($root){
            $this->root = $root;   
        }
        
        
        /**
         * Returns the main string appended to all breadcrumb links
         *
         * @return string
         */
        public function getRoot(){
            return $this->root;   
        }
        
        
        /**
         * Calculates the while breadcrumb path to the current folder and page
         *
         * @param integer $nodeID
         * @param integer $pageID
         */
        public function calculate($nodeID,$pageID=null,$type=null){
            //get data for the selected node
            $myTree = new tree($this->db,'structure LEFT JOIN objectmap ON structure.id = objectmap.nodeid',null,'path_cache AS url, title');
         
            //first get the stack to the node
            $crumbStack = (array)$myTree->getPathStack($nodeID);
            
            foreach($crumbStack as $crumb){
                $this->push($crumb['title'],$crumb['url']);   
            }
            
            //is there a page as well?
            if($pageID and $type != 'tag'){
                $data = $this->db->getRow("SELECT pageurl||'.html' AS url, title FROM pagemap WHERE id = " . (int)$pageID);
                $this->push($data['title'],$crumb['url'].$data['url']);
            }
            

            if($type == 'tag' and $pageID){
                $data = $this->db->getRow("SELECT url||'.html' AS url, name FROM tags WHERE id = " . (int)$pageID);
                $this->push($data['name'],$crumb['url'].$data['url']);
            }
            
            return;
        }
    }
?>